/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Vector;
import base.*;
import trackmsg.EchoMsg;
import trackmsg.TrackCtrlMsg;

/**
 *
 * @author Administrator
 */
public class ASTConcatExecution extends WaveExecution{

    public ASTConcatExecution(WaveHeadParser parser, WaveEnvironment waveEnv){
        super(parser, waveEnv);
    }

    public void execute(ASTConcat concat, String tail) {
        Vector vLeft = getASTNodeValue((SimpleNode)concat.jjtGetChild(0));
        Vector vRight = getASTNodeValue((SimpleNode)concat.jjtGetChild(1));
        Utilities.WPPrint(vLeft + " " + vRight);
        Vector v = WaveActs.concat(vLeft, vRight);
        SimpleNode left = (SimpleNode)concat.jjtGetChild(0);
            
        if (left instanceof ASTFrontalVar) {
            Hashtable fTab = this.waveEnv.getFrontalTable();
            fTab.put(left.getString(), v);
        }

        else if (left instanceof ASTNodalVar) {
            Hashtable nTab = this.kn.getKNNode(
                    this.waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
            nTab.put(left.getString(), v);
        }

        if (waveEnv.getTrackAddress() != null && tail.equals("")) {
            //Neu tail ma rong thi gui echo la TRUE di
            TrackNode fromTrack = wi.getTrackForest().getTrackNode(waveEnv.getTrackAddress());
            TrackNode.Echo echo = TrackNode.Echo.TRUE;
            //Tao message echo va gui vao TrackQueue
            //Utilities.WPPrint("ME");
            TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
            wi.getTrackQueue().incoming(msg);
        } else if(!tail.equals("")){
            Wave newWave = new Wave(tail, new WaveEnvironment(waveEnv));
            waveQueue.incomingFront(newWave);
        }
        waveEnv.dump();
        Utilities.WPPrint("Concat execution Done!");
    }

    private Vector getChild(ASTConcat concat, int intPos)
    {
        SimpleNode child = (SimpleNode) concat.jjtGetChild(intPos);
        Vector v = new Vector();
        Object obj = new Object();
        if (child instanceof ASTFrontalVar){
            obj = waveEnv.getFrontalTable().get(child.getString());
            //Utilities.WPPrint("OBJ VALUE: " + obj);
        } else if (child instanceof ASTNodalVar){
            Hashtable<String, Vector> nodalTable =
                    kn.getKNNode(waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
            obj = nodalTable.get(child.getString());
        }
        else if (child instanceof ASTContent) {
            obj = waveEnv.getCONTENT();
        }
        else {
            obj = child.getString();
        }
        if (obj instanceof Collection){
            v.addAll((Collection)obj);
        } else if (obj != null) {

            v.add(obj);
        }
        return  v;
    }

    public Vector getLeftChild(ASTConcat concat){
        return getChild(concat, 0);
    }

    public Vector getRightChild(ASTConcat concat){
        return getChild(concat, 1);
    }

}
